/**
 * This is the invoice viewer.
 * @author Suren Nihalani
 */

// $codepro.audit.disable numericLiterals
package edu.gatech.coc.betameisters.userinterfaces;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;

import edu.gatech.coc.betameisters.infoholders.Invoice;
import edu.gatech.coc.betameisters.infoholders.Constants;
import edu.gatech.coc.betameisters.main.MainPanel;
import edu.gatech.coc.betameisters.people.Doctor;
import edu.gatech.coc.betameisters.people.Nurse;
import edu.gatech.coc.betameisters.people.Person;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * This is a JPanel to view an invoice object.
 * 
 * @version 1.0
 * @author Suren Nihalani
 */
public class InvoiceView extends JPanel implements Printable {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;

	/** The invoice to be printed. */
	private final Invoice theInvoice;

	/** Font size of the billing table. */
	private static final int FONT_SIZE = 16;

	/**
	 * Creates the panel.
	 * 
	 * @param toBePrinted
	 *            The Invoice to be viewed/printed.
	 * @param parent
	 *            Parent of this UI. Needed for swiping panels
	 */
	public InvoiceView(final Invoice toBePrinted, final MainPanel parent) { // $codepro.audit.disable
																		// com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods
		theInvoice = toBePrinted;
		setLayout(null);
		final JLabel lblNewLabel = new JLabel(toBePrinted.getHospitalName());
		lblNewLabel.setFont(new Font("Lucida Grande", Font.PLAIN, FONT_SIZE));
		lblNewLabel.setBounds(194, 6, 350, 16);
		add(lblNewLabel);

		final JLabel lblPatientName = new JLabel("Patient Name:");
		lblPatientName.setBounds(6, 34, 92, 16);
		add(lblPatientName);

		final JLabel lblName = new JLabel(toBePrinted.getPatientName());
		lblName.setBounds(139, 34, 283, 16);
		add(lblName);

		final JLabel lblDate = new JLabel(toBePrinted.getDate());
		lblDate.setBounds(434, 34, 104, 16);
		add(lblDate);

		final JLabel lblConsultingDoctor = new JLabel("Consulting Doctor:");
		lblConsultingDoctor.setBounds(6, 62, 120, 16);
		add(lblConsultingDoctor);

		final JLabel lblMydoctor = new JLabel(toBePrinted.getDoctor());
		lblMydoctor.setBounds(141, 62, 283, 16);
		add(lblMydoctor);

		final JLabel lblParticulars = new JLabel("Particulars:");
		lblParticulars.setBounds(6, 90, 92, 16);
		add(lblParticulars);

		final JLabel lblTotalamount = new JLabel("Total Amount");
		lblTotalamount.setBounds(279, 466, 92, 16);
		add(lblTotalamount);

		final JLabel lblNewLabel_1 = new JLabel(toBePrinted.getTotalAmount());
		lblNewLabel_1.setBounds(383, 466, 155, 16);
		add(lblNewLabel_1);

		final JLabel lblNurse = new JLabel("Nurse:");
		lblNurse.setBounds(37, 466, 61, 16);
		add(lblNurse);

		final JLabel lblNurse_1 = new JLabel(toBePrinted.getMyNurse());
		lblNurse_1.setBounds(110, 466, 157, 16);
		add(lblNurse_1);

		final JPanel panel = new JPanel();
		panel.setBounds(88, 118, 432, 336);
		add(panel);

		final JTable table_3 = new JTable();/*
		table_3.setModel(toBePrinted.getParticulars().getModel());
		table_3.setFillsViewportHeight(true);
		table_3.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		table_3.setFocusable(false);
		table_3.setCellSelectionEnabled(false);
		table_3.setRowSelectionAllowed(false);
		table_3.setPreferredSize(new Dimension(300, 300));
		table_3.setBounds(0, 0, 300, 300);
		table_3.setShowVerticalLines(true);
		table_3.setGridColor(Color.black);
		table_3.setFont(new Font(Font.SANS_SERIF, Font.ITALIC, 16));
		panel.add(table_3);
*/
		final JMenuBar menuBar = new JMenuBar();
		menuBar.setBounds(-2, 0, 128, 21);
		add(menuBar);

		final JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		final JMenuItem mntmPrint = new JMenuItem("Print");
		mntmPrint.addActionListener(new buttonListener(parent));
		mnFile.add(mntmPrint);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				Person aPer = parent.getUser();
				if(aPer.getUserType() == Constants.UserType.DOCTOR){
					parent.setDoctor((Doctor) aPer);
				}
				else{
					parent.setNurse((Nurse) aPer);
				}
			}
		});
		btnCancel.setBounds(455, 526, 89, 23);
		add(btnCancel);
	}

	/**
	 * Method print.
	 * 
	 * @param g
	 *            Graphics
	 * @param pf
	 *            PageFormat
	 * @param arg2
	 *            int
	 * 
	 * 
	 * @return int * @see java.awt.print.Printable#print(Graphics, PageFormat,
	 *         int)
	 */
	public int print(Graphics g, PageFormat pf, int arg2) {
		if (arg2 > 0) {
			return NO_SUCH_PAGE;
		}
		final Graphics2D g2 = (Graphics2D) g;
		g2.translate(pf.getImageableX(), pf.getImageableY());

		final boolean wasBuffered = isDoubleBuffering(this);
		super.paintAll(g2);
		this.paintAll(g2);

		restoreDoubleBuffering(this, wasBuffered);
		return PAGE_EXISTS;
	}

	/**
	 * @see java.awt.Component#toString()
	 */
	public String toString() {
		return "Invoice of " + theInvoice.getPatientName();
	}

	/**
	 * Method isDoubleBuffering.
	 * 
	 * @param c
	 *            JComponent whose state is to be checked
	 * 
	 * @return boolean
	 */
	private boolean isDoubleBuffering(JComponent c) {
		final boolean wasBuffered = (c).isDoubleBuffered();
		(c).setDoubleBuffered(false);
		return wasBuffered;
	}

	/**
	 * Method restoreDoubleBuffering.
	 * 
	 * @param c
	 *            JComponent
	 * @param wasBuffered
	 *            boolean to restore previous state
	 */
	private void restoreDoubleBuffering(JComponent c, boolean wasBuffered) {
		(c).setDoubleBuffered(wasBuffered);
	}

	private class buttonListener implements ActionListener {
		private MainPanel p;

		public buttonListener(MainPanel parent) {
			p = parent;
		}

		public void actionPerformed(final ActionEvent arg0) {
			final PrinterJob printJob = PrinterJob.getPrinterJob();
			final InvoiceView v = new InvoiceView(theInvoice, p);
			printJob.setPrintable(v);
			if (printJob.printDialog()) { // $codepro.audit.disable
											// nullPointerDereference
				try {
					printJob.print();
				} catch (final PrinterException pe) {
					System.out.println("Error printing: " + pe);
				}
			}
		}
	}

}
